home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / pine / osdep / print.os2 < prev    next >
Text File  |  1996-05-23  |  10KB  |  390 lines

  1. #line 2 "osdep/print.os2"
  2. /*======================================================================
  3.     print routines
  4.    
  5.     Functions having to do with printing on paper and forking of spoolers
  6.  
  7.     In general one calls open_printer() to start printing. One of
  8.     the little print functions to send a line or string, and then
  9.     call print_end() when complete. This takes care of forking off a spooler
  10.     and piping the stuff down it. No handles or anything here because there's
  11.     only one printer open at a time.
  12.  
  13.  ====*/
  14.  
  15.  
  16. static char *trailer; /* so both open and close_printer can see it */
  17.  
  18. /*----------------------------------------------------------------------
  19.        Open the printer
  20.  
  21.   Args: desc --Description of item to print. Should have on tailing blank.
  22.  
  23. This does most of the work of popen so we can save the standard output of the
  24. command we execute and send it back to the user.
  25.   ----*/
  26. int
  27. open_printer(desc)
  28.      char *desc;
  29. {
  30.     char command[201], prompt[200];
  31.     int  cmd, rc, just_one;
  32.     char *p, *init, *nick;
  33.     char aname[100];
  34.     char *printer;
  35.     int     done = 0, i, lastprinter, cur_printer = 0;
  36.     HelpType help;
  37.     char   **list;
  38.     static ESCKEY_S ekey[] = {
  39.     {'y', 'y', "Y", "Yes"},
  40.     {'n', 'n', "N", "No"},
  41.     {ctrl('P'), 10, "^P", "Prev Printer"},
  42.     {ctrl('N'), 11, "^N", "Next Printer"},
  43.     {-2,   0,   NULL, NULL},
  44.     {'c', 'c', "C", "CustomPrint"},
  45.     {KEY_UP,    10, "", ""},
  46.     {KEY_DOWN,  11, "", ""},
  47.     {-1, 0, NULL, NULL}};
  48. #define PREV_KEY   2
  49. #define NEXT_KEY   3
  50. #define CUSTOM_KEY 5
  51. #define UP_KEY     6
  52. #define DOWN_KEY   7
  53.  
  54.     trailer      = NULL;
  55.     init         = NULL;
  56.     nick         = NULL;
  57.     command[200] = '\0';
  58.  
  59.     if(ps_global->VAR_PRINTER == NULL){
  60.         q_status_message(SM_ORDER | SM_DING, 3, 5,
  61.     "No printer has been chosen.  Use SETUP on main menu to make choice.");
  62.     return(-1);
  63.     }
  64.  
  65.     /* Is there just one print command available? */
  66.     just_one = (ps_global->printer_category!=3&&ps_global->printer_category!=2)
  67.            || (ps_global->printer_category == 2
  68.            && !(ps_global->VAR_STANDARD_PRINTER
  69.             && ps_global->VAR_STANDARD_PRINTER[0]
  70.             && ps_global->VAR_STANDARD_PRINTER[1]))
  71.            || (ps_global->printer_category == 3
  72.            && !(ps_global->VAR_PERSONAL_PRINT_COMMAND
  73.             && ps_global->VAR_PERSONAL_PRINT_COMMAND[0]
  74.             && ps_global->VAR_PERSONAL_PRINT_COMMAND[1]));
  75.  
  76.     if(F_ON(F_CUSTOM_PRINT, ps_global))
  77.       ekey[CUSTOM_KEY].ch = 'c'; /* turn this key on */
  78.     else
  79.       ekey[CUSTOM_KEY].ch = -2;  /* turn this key off */
  80.  
  81.     if(just_one){
  82.     ekey[PREV_KEY].ch = -2;  /* turn these keys off */
  83.     ekey[NEXT_KEY].ch = -2;
  84.     ekey[UP_KEY].ch   = -2;
  85.     ekey[DOWN_KEY].ch = -2;
  86.     }
  87.     else{
  88.     ekey[PREV_KEY].ch = ctrl('P'); /* turn these keys on */
  89.     ekey[NEXT_KEY].ch = ctrl('N');
  90.     ekey[UP_KEY].ch   = KEY_UP;
  91.     ekey[DOWN_KEY].ch = KEY_DOWN;
  92.     /*
  93.      * count how many printers in list and find the default in the list
  94.      */
  95.     if(ps_global->printer_category == 2)
  96.       list = ps_global->VAR_STANDARD_PRINTER;
  97.     else
  98.       list = ps_global->VAR_PERSONAL_PRINT_COMMAND;
  99.  
  100.     for(i = 0; list[i]; i++)
  101.       if(strcmp(ps_global->VAR_PRINTER, list[i]) == 0)
  102.         cur_printer = i;
  103.     
  104.     lastprinter = i - 1;
  105.     }
  106.  
  107.     help = NO_HELP;
  108.     ps_global->mangled_footer = 1;
  109.  
  110.     while(!done){
  111.     if(init)
  112.       fs_give((void **)&init);
  113.  
  114.     if(trailer)
  115.       fs_give((void **)&trailer);
  116.  
  117.     if(just_one)
  118.       printer = ps_global->VAR_PRINTER;
  119.     else
  120.       printer = list[cur_printer];
  121.  
  122.     parse_printer(printer, &nick, &p, &init, &trailer, NULL, NULL);
  123.     strncpy(command, p, 200);
  124.     fs_give((void **)&p);
  125.     sprintf(prompt, "Print %sto \"%s\" ? ", desc ? desc : "",
  126.         *nick ? nick : command);
  127.  
  128.     fs_give((void **)&nick);
  129.     
  130.     cmd = radio_buttons(prompt, -FOOTER_ROWS(ps_global),
  131.                  ekey, 'y', 'x', help, RB_NORM);
  132.     
  133.     switch(cmd){
  134.       case 'y':
  135.         q_status_message1(SM_ORDER, 0, 9,
  136.         "Printing to \"%s\"", command);
  137.         done++;
  138.         break;
  139.  
  140.       case 10:
  141.         cur_printer = (cur_printer>0)
  142.                 ? (cur_printer-1)
  143.                 : lastprinter;
  144.         break;
  145.  
  146.       case 11:
  147.         cur_printer = (cur_printer<lastprinter)
  148.                 ? (cur_printer+1)
  149.                 : 0;
  150.         break;
  151.  
  152.       case 'n':
  153.       case 'x':
  154.         done++;
  155.         break;
  156.  
  157.       case 'c':
  158.         done++;
  159.         break;
  160.  
  161.       default:
  162.         break;
  163.     }
  164.     }
  165.  
  166.     if(cmd == 'c'){
  167.     if(init)
  168.       fs_give((void **)&init);
  169.  
  170.     if(trailer)
  171.       fs_give((void **)&trailer);
  172.  
  173.     sprintf(prompt, "Enter printer port : ");
  174.     command[0] = '\0';
  175.     rc = 1;
  176.     while(rc){
  177.         rc = optionally_enter(command, -FOOTER_ROWS(ps_global), 0,
  178.         200, 1, 0, prompt, NULL, NO_HELP, 0);
  179.         
  180.         if(rc == 1){
  181.         cmd = 'x';
  182.         rc = 0;
  183.         }
  184.         else if(rc == 3)
  185.           q_status_message(SM_ORDER, 0, 3, "No help available");
  186.         else if(rc == 0){
  187.         removing_trailing_white_space(command);
  188.         removing_leading_white_space(command);
  189.         q_status_message1(SM_ORDER, 0, 9,
  190.             "Printing to \"%s\"", command);
  191.         }
  192.     }
  193.     }
  194.  
  195.     if(cmd == 'x' || cmd == 'n'){
  196.     q_status_message(SM_ORDER, 0, 2, "Print cancelled");
  197.     if(init)
  198.       fs_give((void **)&init);
  199.  
  200.     if(trailer)
  201.       fs_give((void **)&trailer);
  202.  
  203.     return(-1);
  204.     }
  205.  
  206.     display_message('x');
  207.  
  208.     ps_global->print = (PRINT_S *)fs_get(sizeof(PRINT_S));
  209.     memset(ps_global->print, 0, sizeof(PRINT_S));
  210.  
  211.     /*----------- Print to selected port, file or pipe ------------*/
  212.     if (*command == '|') {    /* pipe */
  213.       ps_global->print->ispipe = 1;
  214.       strcpy(command, command+1);
  215.       dprint(4, (debugfile, "Printing using command \"%s\"\n", command));
  216.       ps_global->print->result = temp_nam(NULL, "pine_prt");
  217.       if(ps_global->print->pipe = open_system_pipe(command,
  218.                      &ps_global->print->result, NULL,
  219.                      PIPE_WRITE | PIPE_STDERR)){
  220.     ps_global->print->fp = ps_global->print->pipe->out.f;
  221.       }
  222.       else{
  223.     fs_give((void *)&ps_global->print->result);
  224.     q_status_message1(SM_ORDER | SM_DING, 3, 4,
  225.               "Error opening printer: %s",
  226.               error_description(errno));
  227.     dprint(2, (debugfile, "Error popening printer \"%s\"\n",
  228.            error_description(errno)));
  229.     if(init)
  230.       fs_give((void **)&init);
  231.  
  232.     if(trailer)
  233.       fs_give((void **)&trailer);
  234.         
  235.     return(-1);
  236.       }
  237.     }
  238.     else{
  239.       ps_global->print->ispipe = 0;
  240.       dprint(4, (debugfile, "Printing to \"%s\"\n", command));
  241.       if ((ps_global->print->fp = fopen(command, "wb")) == NULL) {
  242.     q_status_message1(SM_ORDER | SM_DING, 3, 4,
  243.               "Error opening printer: %s",
  244.               error_description(errno));
  245.     dprint(2, (debugfile, "Error opening printer \"%s\"\n",
  246.            error_description(errno)));
  247.     if(init)
  248.       fs_give((void **)&init);
  249.  
  250.     if(trailer)
  251.       fs_give((void **)&trailer);
  252.         
  253.     return(-1);
  254.       }
  255.     }
  256.  
  257.     ps_global->print->err = 0;
  258.  
  259.     if(init){
  260.     if(*init)
  261.       fputs(init, ps_global->print->fp);
  262.  
  263.     fs_give((void **)&init);
  264.     }
  265.  
  266.     return(0);
  267. }
  268.  
  269.  
  270.  
  271. /*----------------------------------------------------------------------
  272.      Close printer
  273.   
  274.   If we're piping to a spooler close down the pipe and wait for the process
  275. to finish. If we're sending to an attached printer send the escape sequence.
  276. Also let the user know the result of the print
  277.  ----*/
  278. void
  279. close_printer()
  280. {
  281.     if(trailer){
  282.     if(*trailer)
  283.       fputs(trailer, ps_global->print->fp);
  284.  
  285.     fs_give((void **)&trailer);
  286.     }
  287.  
  288.     if (ps_global->print->ispipe) {
  289.       (void) close_system_pipe(&ps_global->print->pipe);
  290.     }
  291.     else{
  292.       (void) fclose(ps_global->print->fp);
  293.     }
  294.     if (ps_global->print->result)
  295.       display_output_file(ps_global->print->result, "PRINT", NULL);
  296.     fs_give((void *)&ps_global->print->result);
  297.     fs_give((void **)&ps_global->print);
  298.  
  299.     q_status_message(SM_ASYNC, 0, 3, "Print command completed");
  300.     display_message('x');
  301. }
  302.  
  303.  
  304.  
  305. /*----------------------------------------------------------------------
  306.      Print a single character
  307.  
  308.   Args: c -- char to print
  309.   Returns: 1 on success, 0 on ps_global->print->err
  310.  ----*/
  311. int
  312. print_char(c)
  313.     int c;
  314. {
  315.     if(!ps_global->print->err && putc(c, ps_global->print->fp) == EOF)
  316.       ps_global->print->err = 1;
  317.  
  318.     return(!ps_global->print->err);
  319. }
  320.  
  321.  
  322.  
  323. /*----------------------------------------------------------------------
  324.      Send a line of text to the printer
  325.  
  326.   Args:  line -- Text to print
  327.  
  328.   ----*/
  329. void
  330. print_text(line)
  331.     char *line;
  332. {
  333.     if(!ps_global->print->err && fputs(line, ps_global->print->fp) == EOF)
  334.       ps_global->print->err = 1;
  335. }
  336.  
  337.  
  338.  
  339. /*----------------------------------------------------------------------
  340.       printf style formatting with one arg for printer
  341.  
  342.  Args: line -- The printf control string
  343.        a1   -- The 1st argument for printf
  344.  ----*/
  345. void
  346. print_text1(line, a1)
  347.     char *line, *a1;
  348. {
  349.     if(!ps_global->print->err
  350.        && fprintf(ps_global->print->fp, line, a1) < 0)
  351.       ps_global->print->err = 1;
  352. }
  353.  
  354.  
  355.  
  356. /*----------------------------------------------------------------------
  357.       printf style formatting with one arg for printer
  358.  
  359.  Args: line -- The printf control string
  360.        a1   -- The 1st argument for printf
  361.        a2   -- The 2nd argument for printf
  362.  ----*/
  363. void
  364. print_text2(line, a1, a2)
  365.     char *line, *a1, *a2;
  366. {
  367.     if(!ps_global->print->err
  368.        && fprintf(ps_global->print->fp, line, a1, a2) < 0)
  369.       ps_global->print->err = 1;
  370. }
  371.  
  372.  
  373.  
  374. /*----------------------------------------------------------------------
  375.       printf style formatting with one arg for printer
  376.  
  377.  Args: line -- The printf control string
  378.        a1   -- The 1st argument for printf
  379.        a2   -- The 2nd argument for printf
  380.        a3   -- The 3rd argument for printf
  381.  ----*/
  382. void
  383. print_text3(line, a1, a2, a3)
  384.     char *line, *a1, *a2, *a3;
  385. {
  386.     if(!ps_global->print->err
  387.        && fprintf(ps_global->print->fp, line, a1, a2, a3) < 0)
  388.       ps_global->print->err = 1;
  389. }
  390.